package com.sanrenzu.chunbo.ui.vertical.fenlei;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.sanrenzu.chunbo.R;
import com.sanrenzu.chunbo.adapter.LikeAdapter;
import com.sanrenzu.chunbo.adapter.ShangPinAdapter;
import com.sanrenzu.chunbo.adapter.XiangQingFramentAdapter;
import com.sanrenzu.chunbo.bean.zxb.DianJiLikeBean;
import com.sanrenzu.chunbo.bean.zxb.ShouYeDianJiBean;
import com.sanrenzu.chunbo.config.UrlConfig;
import com.sanrenzu.chunbo.dao.Collection;
import com.sanrenzu.chunbo.dao.ShopDao;
import com.sanrenzu.chunbo.ui.fenlei.xiangqing.XiangQingFragment;
import com.sanrenzu.chunbo.widget.MyGirdView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

public class FenLeiActivity extends AppCompatActivity implements FenLeiContractA.View {

    //VIEWPAGER 图片及下方价格   控件初始化
    @BindView(R.id.fenlei_viewpager_main)
    ViewPager fenleiViewpagerMain;
    @BindView(R.id.fenlei_jiashao_main)
    TextView fenleiJiashaoMain;
    @BindView(R.id.fenlei_jiashao_main2)
    TextView fenleiJiashaoMain2;
    @BindView(R.id.fenlei_jiage_main)
    TextView fenleiJiageMain;
    @BindView(R.id.fenlei_chunbojiage_main)
    TextView fenleiChunbojiageMain;
    @BindView(R.id.fenlei_girdview_main)
    MyGirdView fenleiGirdviewMain;

    //图文详情页 控件初始化
    @BindView(R.id.fenlei_second_viewpager)
    ViewPager fenleiSecondViewpager;
    @BindView(R.id.fenlei_second_tablayout)
    TabLayout fenleiSecondTablayout;
    @BindView(R.id.join_shopping_cart)
    Button joinShoppingCart;
    @BindView(R.id.fenlei_btn_fenxiang)
    Button fenleiBtnFenxiang;

    private ShangPinAdapter shangPinAdapter;
    private LikeAdapter likeAdapter;
    //persenter内部方法调用
    private FenLeiContractA.Persenter persenterA;
    // 黄油刀对象声明
    private Unbinder bind;
    private Context context = this;
    //tablayout标题设置
    private String[] arrayTabs;
    //动态创建碎片
    private List<Fragment> mListFragment = new ArrayList<>();
    //详情页适配器
    private XiangQingFramentAdapter xiangQingFramentAdapter;

    public String id = "";
    private AlertDialog.Builder builder;
    private List<DianJiLikeBean> productRecommendedList;
    private ShouYeDianJiBean shouYeDianJiBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fen_lei);
        ShareSDK.initSDK(FenLeiActivity.this);
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        Map<String, String> map = new HashMap<>();
        map.put(UrlConfig.Key.PRODUCT_ID, id);

        bind = ButterKnife.bind(this);
        persenterA = new FenLeiPersenterA(this);
        persenterA.getVerticalShouYe(map);

        initView();


    }

    @Override
    public void onGetVerticalDianJiTuPianBean(final ShouYeDianJiBean dianJiTuPianBean) {

        List<String> productContentImagesWH = dianJiTuPianBean.getProductImages();
        productRecommendedList = dianJiTuPianBean.getProductRecommendedList();
        //viewpager下面的属性控件声明
        fenleiJiashaoMain.setText(dianJiTuPianBean.getAdTitle());
        fenleiJiashaoMain2.setText(dianJiTuPianBean.getProductName());
        fenleiJiageMain.setText(dianJiTuPianBean.getProductMarketPrice() + "");
        fenleiChunbojiageMain.setText(dianJiTuPianBean.getProductHotPrice() + "");
        //设置适配器，viewpager和猜你喜欢
        shangPinAdapter = new ShangPinAdapter(context, productContentImagesWH);
        likeAdapter = new LikeAdapter(context, productRecommendedList);

        fenleiGirdviewMain.setAdapter(likeAdapter);
        fenleiViewpagerMain.setAdapter(shangPinAdapter);

        joinShoppingCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                builder = new AlertDialog.Builder(FenLeiActivity.this)
                        .setMessage("是否加入购物车")
                        .setNegativeButton("继续购物", null)
                        .setPositiveButton("添加购物车", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String productId = dianJiTuPianBean.getProductId();
                                String productName = dianJiTuPianBean.getProductName();
                                String productMarketPrice = dianJiTuPianBean.getProductMarketPrice();
                                List<String> productImages = dianJiTuPianBean.getProductImages();

                                Collection collection = new Collection();

                                collection.setProductId(productId);
                                collection.setProductName(productName);
                                collection.setProductMarketPrice(productMarketPrice);
                                collection.setProduct_images(productImages.get(0));
                                List<Collection> collections = ShopDao.quaryOne(FenLeiActivity.this, dianJiTuPianBean.getProductId());
                                if (collections.size() == 0) {
                                    ShopDao.add(collection, FenLeiActivity.this);
                                } else {
                                    Toast.makeText(FenLeiActivity.this, "已经有了", Toast.LENGTH_SHORT);
                                }
                            }
                        });
                builder.show();
            }
        });
    }

    private void initView() {
        arrayTabs = getResources().getStringArray(R.array.tabTiles);
        for (int i = 0; i < arrayTabs.length; i++) {
            XiangQingFragment fragment = XiangQingFragment.newInstance(i);
            mListFragment.add(fragment);
        }
        xiangQingFramentAdapter = new XiangQingFramentAdapter(getSupportFragmentManager(), mListFragment, arrayTabs);
        fenleiSecondViewpager.setAdapter(xiangQingFramentAdapter);
        fenleiSecondTablayout.setupWithViewPager(fenleiSecondViewpager);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        bind.unbind();
    }

    private void showShare() {
        ShareSDK.initSDK(this);
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间等使用
        oks.setTitle("标题");
        // titleUrl是标题的网络链接，QQ和QQ空间等使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

// 启动分享GUI
        oks.show(this);
    }


    @OnClick(R.id.fenlei_btn_fenxiang)
    public void onClick() {
        showShare();
    }
}
